package com.liangfeng.breezebrowser.browsermvp.module;

import com.apkfuns.logutils.LogUtils;
import com.liangfeng.breezebrowser.KeyWordEvent;
import com.liangfeng.breezebrowser.base.BrowserApplication;
import com.liangfeng.breezebrowser.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;

/**
 * Created by mozhifeng on 2017/5/25.
 */

public class BrowserModule implements IBrowser {

    private String result = "";

    @Override
    public void getKeyWordHint(String content) {
        // http://suggestion.baidu.com/su?wd=+"关键字"+&p=3&cb=window.bdsug.sug
        String urlStart = "http://suggestion.baidu.com/su?wd=";
        String urlEnd = "&p=3&cb=window.bdsug.sug";
        String url = urlStart + content + urlEnd;
//        LogUtils.e(url);
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new NetCallBack());
    }


    private class NetCallBack extends StringCallback {

        @Override
        public void onError(Call call, Exception e, int id) {
            ToastUtil.showShort(BrowserApplication.context, "访问失败，请检查网络");
        }

        @Override
        public void onResponse(String response, int id) {
            LogUtils.e(response);
            setResult(response);
            EventBus.getDefault().post(new KeyWordEvent(response));
        }
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
}
